#region Usings

using MsForgeReboot.Web.App_Start;

using WebActivator;

#endregion

[assembly: PreApplicationStartMethod(typeof(SimpleMembershipMvc3), "Start")]
[assembly: PostApplicationStartMethod(typeof(SimpleMembershipMvc3), "Initialize")]

namespace MsForgeReboot.Web.App_Start {
    #region Usings

    using System.Collections.Specialized;
    using System.Configuration;
    using System.Web.Mvc;
    using System.Web.Security;

    using MsForgeReboot.Web.Services;

    using WebMatrix.WebData;

    #endregion

    public static class SimpleMembershipMvc3 {
        #region Constants and Fields

        public static readonly string EnableSimpleMembershipKey = "enableSimpleMembership";

        #endregion

        #region Public Properties

        public static bool SimpleMembershipEnabled {
            get {
                return IsSimpleMembershipEnabled();
            }
        }

        #endregion

        #region Public Methods and Operators

        public static void Initialize() {
            // Modify the settings below as appropriate for your application
            WebSecurity.InitializeDatabaseConnection(
                connectionStringName: "MsForgeContext",
                userTableName: "Users",
                userIdColumn: "Id",
                userNameColumn: "Username",
                autoCreateTables: false);

            // Comment the line above and uncomment these lines to use the IWebSecurityService abstraction
            //var webSecurityService = DependencyResolver.Current.GetService<IWebSecurityService>();
            //webSecurityService.InitializeDatabaseConnection(connectionStringName: "MsForgeContext", userTableName: "Users", userIdColumn: "ID", userNameColumn: "Username", autoCreateTables: true);
        }

        public static void Start() {
            if (SimpleMembershipEnabled) {
                var provider = Membership.Providers["AspNetSqlMembershipProvider"];
                if (provider != null) {
                    var currentDefault = provider;
                    var provider2 = CreateDefaultSimpleMembershipProvider("AspNetSqlMembershipProvider", currentDefault);
                    Membership.Providers.Remove("AspNetSqlMembershipProvider");
                    Membership.Providers.Add(provider2);
                }
                Roles.Enabled = true;
                var provider3 = Roles.Providers["AspNetSqlRoleProvider"];
                if (provider3 != null) {
                    var provider6 = provider3;
                    var provider4 = CreateDefaultSimpleRoleProvider("AspNetSqlRoleProvider", provider6);
                    Roles.Providers.Remove("AspNetSqlRoleProvider");
                    Roles.Providers.Add(provider4);
                }
            }
        }

        #endregion

        #region Methods

        private static SimpleMembershipProvider CreateDefaultSimpleMembershipProvider(
            string name, MembershipProvider currentDefault) {
            var previousProvider = currentDefault;
            var provider = new SimpleMembershipProvider(previousProvider);
            var config = new NameValueCollection();
            provider.Initialize(name, config);
            return provider;
        }

        private static SimpleRoleProvider CreateDefaultSimpleRoleProvider(string name, RoleProvider currentDefault) {
            var previousProvider = currentDefault;
            var provider = new SimpleRoleProvider(previousProvider);
            var config = new NameValueCollection();
            provider.Initialize(name, config);
            return provider;
        }

        private static bool IsSimpleMembershipEnabled() {
            bool flag;
            var str = ConfigurationManager.AppSettings[EnableSimpleMembershipKey];
            if (!string.IsNullOrEmpty(str) && bool.TryParse(str, out flag)) {
                return flag;
            }
            return true;
        }

        #endregion
    }
}